home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / appe Windows 2.03 / filter.c < prev    next >
Encoding:
Text File  |  1995-12-05  |  11.8 KB  |  288 lines  |  [TEXT/CWIE]

  1. // File "filter.c" -
  2.  
  3. #include <TextServices.h>
  4.  
  5. #include "main.h"
  6. #include "filter.h"
  7. #include "floaters.h"
  8.  
  9. // ***********************************************************************************
  10. // Global Declarations 
  11.  
  12. extern GlobalsRec glob;
  13.  
  14. // ***********************************************************************************
  15. // ***********************************************************************************
  16.  
  17. /*
  18.     The Not-So-Simple FAT jGNEFilter 
  19.         Original Code by Matt Slot (fprefect@umich.edu), 6/2/95 
  20.             with help and criticism (lots!) from Ed Wynne (arwyn@umich.edu).
  21.     
  22.     Quick Intro
  23.         Since jGNEFilters are nasty things in 68k, and pretty much impossible 
  24.         in PPC, writing simple and cross-compiling handler code is also. To 
  25.         facilitate use of this, I have written some interface routines to hide
  26.         some of the complexity from the application programmer.
  27.         
  28.     /    Ptr InstallEventFilter(FilterHelperUPP helperProc, Ptr helperData);
  29.     |        Installs a jGNEFilter which can properly call a callback from within
  30.     |        the application. The callback handles incoming events wither either 
  31.     |        68k or PPC code, and gets passed the data pointer you send from here.
  32.     |        Install() returns a pointer the jGNEFilter (which has been loaded from
  33.     |        a resource) installed in the system heap -- or NIL to indicate an error.
  34.  
  35.     /    Ptr ReleaseEventFilter(Ptr filterProc);
  36.     |        Pass in the pointer to the jGNEFilter, and this routine will disable 
  37.     |        the handling and try to dispose of its storage in the System Heap.
  38.     |        You *must* do this if your helper function is going to disappear when
  39.     |        the application closes. 
  40.     
  41.     /    Code Resource 'jGNE', #128: the jGNEFilter
  42.     |        Since there is no way to get the same routine to compile on both 68K
  43.     |        and PPC, I have compiled the jGNEFilter into an executable resource.
  44.     |        The real code of this resource has been compiled from the additional
  45.     |        source file you can find in the project folder. Note that the code simply  
  46.     |        calls the ProcPtr for the Helper function blindly... whether 68k or PPC. 
  47.     |        See below for more details as to how the filter is loaded, unloaded,
  48.     |        and called.
  49.  
  50.     /    void EventFilterHelper(EventRecord *theEvent, Ptr helperData);
  51.     |        This callback is the workhorse of the event filter. Once installed, this  
  52.     |        routine sees every event that gets harvested and has an opportunity to
  53.     |        modify the record before the front application gets to see it. Ideally
  54.     |        this function can do the necessary work itself or pass off the event info
  55.     |        to the home application.
  56.     |        WARNING: For 68k code, the routine will be called from the current app's
  57.     |        context (A5/Globals, Rsrc File, HeapZone). PPC code will have a valid RTOC
  58.     |        (Globals access) but not Rsrc File, HeapZone, etc.
  59.     
  60.  
  61.     How this all works:
  62.         
  63.         Since I didn't want to write the jGNEFilter in C (OK, I couldn't figure out
  64.         a good way), the code is carried around in a resource. Originally, I had 
  65.         saved the routine as a constant string that was StuffHex()'ed into the System 
  66.         Heap pointer... but I got so many questions and comments I decided to do it 
  67.         the normal way.
  68.         
  69.         The jGNEFilter keeps 3 pieces of data inline: the next filter in the chain,
  70.         a pointer to the helper routine, and some extra data to pass to the helper.
  71.         Most importantly, we may not be able to remove the filter from the calling
  72.         chain...  the architecture just doesn't permit it! If we are able to safely
  73.         pull the filter out, we do. Otherwise the next best solution is to keep a flag,
  74.         that we can clear when we want to disable the functionality -- in fact, we
  75.         set or clear the pointer to the Helper Proc as the flag.
  76.         
  77.         Finally, the helper function is the meat of our jGNEFilter; it does the work
  78.         of the active filter. In the case of a 68K helper, it is accessed via a 
  79.         simple ProcPtr. In the case of a PPC helper, the installer sets up a valid
  80.         RoutineDescriptor (in the System Heap with the jGNEFilter) to invoke a 
  81.         MixedMode switch between the 68K caller (filter) and PPC routine (helper).
  82.         Again, when releasing the filter the handler disposes the descriptor and 
  83.         clear the inline ProcPtr/flag, since the helper function will probably be
  84.         disappearing when the application quits.
  85.         
  86.         If you are picking out events to handle within your app, my suggestion is to
  87.         keep a secondary Queue of events in the System Heap -- remember, you must 
  88.         allocate new EventRecords (since the current event belongs to the calling
  89.         app) into the System Heap (you need a heap that both the current process and
  90.         your own process can access). Given these events, your main event loop can 
  91.         suck out clicks or keydowns for dispatching internally and safely within your
  92.         own context.
  93.         
  94.         Also, Text Service windows don't receive Activate or Update events... you 
  95.         must check for those manually within your own event loop and handle them.
  96.     
  97. */
  98.  
  99. // ***********************************************************************************
  100. // ***********************************************************************************
  101.  
  102. Ptr InstallEventFilter(FilterHelperUPP helperProc, Ptr helperData) {
  103.     Handle filterRsrc;
  104.     Ptr filterProc, data;
  105.     
  106.     // Create a duplicate function in the System Heap (so its *alway* there) and
  107.     //   copy the data across. Note: even though it is technically a function, we
  108.     //   can still treat it as data safely until it has been installed and invoked.
  109.     filterRsrc = Get1Resource(kJGNEFilterResType, kJGNEFilterResID);
  110.     filterProc = NewPtrSys(GetHandleSize(filterRsrc));
  111.     // If either is nil, there is no need to free.. we are quitting anyway
  112.     if (! filterRsrc || ! filterProc) return(0); 
  113.     BlockMove(*filterRsrc, filterProc, GetHandleSize(filterRsrc));
  114.     ReleaseResource(filterRsrc);
  115.     
  116.     // Get and install the current filter as the next filter in the chain.
  117.     data = (Ptr) LMGetGNEFilter();
  118.     BlockMove(&data, filterProc + kNextFilterOffset, sizeof(data));
  119.  
  120.     // Get and install the Helper function to do the real work (and as a flag to
  121.     //   indicate we are in business and accepting events). Remember that if we
  122.     //   generating PPC code, it is necessary to establish a Routine Descriptor.
  123.     SetZone(SystemZone());
  124.     data = (Ptr) NewFilterHelperProc(helperProc);
  125.     if (! data) return(0);
  126.     BlockMove(&data, filterProc + kEventHelperOffset, sizeof(data));
  127.     SetZone(ApplicZone());
  128.  
  129.     // If the caller wants to pass data to the jGNEFilter Helper function. This
  130.     //   pointer (or handle if desired) *must* be allocated in the System Heap
  131.     //   if you don't plan on releasing the Filter before quitting. If you plan
  132.     //   on releasing the filter, then either the App or Sys heap will suffice.
  133.     data = helperData;
  134.     BlockMove(&helperData, filterProc + kEventHelperDataOffset, sizeof(data));
  135.     
  136.     // Install us, we are ready to do some work!
  137.     LMSetGNEFilter((GNEFilterUPP) filterProc);
  138.     
  139.     return(filterProc);
  140.     }
  141.  
  142. // ***********************************************************************************
  143. // ***********************************************************************************
  144.  
  145. Ptr ReleaseEventFilter(Ptr filterProc) {
  146.     Ptr data;
  147.     
  148.     if (! filterProc) return(0);
  149.  
  150.     // Clear the Helper location as an indicator that we have closed up shop. The
  151.     //   filter itself may lingers in the System Heap until shutdown unless we can
  152.     //   find a way to extract it from the chain (see below). On the other hand, the 
  153.     //   filter has been written so that if the Helper function pointer is NIL, the 
  154.     //   filter will do nothing at all. Let's zero it out for that (hopeful) case.
  155.     BlockMove(filterProc + kEventHelperOffset, &data, sizeof(data));
  156.     if (data) DisposeRoutineDescriptor((UniversalProcPtr) data);
  157.     data = 0;
  158.     BlockMove(&data, filterProc + kEventHelperOffset, sizeof(data));
  159.     
  160.  
  161.     // If the installed filterProc is the first one in the chain, then we should
  162.     //    be able remove it and replace it with the next one (the one we would
  163.     //    normally jump to). If we can dispose the filterProc buffer, then we can
  164.     //    can recover those 50 bytes that remain in the System Heap.
  165.     // Thanks to HoverBar's Guy Fullerton (hedgeboy@realm.net) for the suggestion.
  166.     if (filterProc == (Ptr) LMGetGNEFilter()) {
  167.         // Remove our filterProc from the chain.
  168.         BlockMove(filterProc + kNextFilterOffset, &data, sizeof(data));
  169.         LMSetGNEFilter((GNEFilterUPP) data);
  170.         
  171.         BlockMove(filterProc + kEventHelperDataOffset, &data, sizeof(data));
  172.         DisposePtr(filterProc);
  173.         }
  174.       else {
  175.         // Grab the data that was passed when initialized or as set in the Helper
  176.         //   function. The caller can then deallocate it if desired or necessary.
  177.         
  178.         BlockMove(filterProc + kEventHelperDataOffset, &data, sizeof(data));
  179.         }
  180.     
  181.     return(data);
  182.     }
  183.  
  184. // ***********************************************************************************
  185. // ***********************************************************************************
  186.  
  187. void EventFilterHelper(EventRecord *theEvent, Ptr helperData) {
  188.     Boolean fwdThisEvent = FALSE, filterThisEvent = FALSE;
  189.     long saveA5;
  190.     EvQEl *fwdEvent;
  191.     
  192.     // This only does something in 68K code. We now have access to globals,
  193.     //   which PPC get for free from CFM; however, we won't have access to 
  194.     //   our application's Resource file/chain or HeapZone. Be careful!
  195.     saveA5 = SetA5((long) helperData);
  196.     
  197.     // Check to see if the floaters should all be hidden, then hide or show any
  198.     //   window's as necessary. Note: Update events will be entered/handled in
  199.     //   our context, so let our app get the CPU to handle the event quickly
  200.     TestScreenSaver();
  201.     if (ShowHideFloater(0)) WakeUpProcess(&glob.myPSN);
  202.     
  203.     switch(theEvent->what) {
  204.         case nullEvent:
  205.             break;
  206.         case mouseDown: {
  207.             short thePart, index;
  208.             long anotherA5;
  209.             Boolean found;
  210.             WindowPtr whichWin;
  211.             FloaterQElemPtr floatQElem;
  212.             
  213.             // I have removed the PtInRgn() tests for the floaters, since the problem
  214.             //   with FindServiceWindow() was A5-related. The solution is to restore
  215.             //   the current app's native A5 temporarily. Thanks go to Ammon Skidkmore
  216.             //   (ammon@cs.byu.edu), as well as Andrew Thaler (athaler@umich.edu)
  217.             anotherA5 = SetCurrentA5();
  218.             thePart = FindServiceWindow(theEvent->where, &whichWin);
  219.             SetA5(anotherA5);
  220.  
  221.             // Walk our window list, and test to see if this floater belongs to us
  222.             //   before we try to forward an AppleGuide click to our program. :)
  223.             for(found = FALSE, floatQElem = GetIndFloater(index = 1, FALSE);
  224.                     floatQElem && !found; floatQElem = GetIndFloater(++index, FALSE))
  225.                 if (whichWin == floatQElem->floatWindow) found = TRUE;
  226.             if (! found) break;
  227.             
  228.             theEvent->message = thePart;
  229.             switch(thePart) {
  230.                 case inMenuBar:
  231.                 case inSysWindow:
  232.                     break;
  233.                 case inDrag:
  234.                 case inContent:
  235.                 case inGrow:
  236.                 case inGoAway:
  237.                 case inZoomIn:
  238.                 case inZoomOut:
  239.                     fwdThisEvent = TRUE;
  240.                     filterThisEvent = TRUE;
  241.                     break;
  242.                 }
  243.             }
  244.             break;
  245.         case keyDown:
  246.         case autoKey: {
  247.             char theKey, theChar;
  248.             
  249.             theChar = theEvent->message & charCodeMask;
  250.             theKey = (theEvent->message & keyCodeMask) >> 8;
  251.             
  252.             // Add your selected tests for key-shortcuts
  253.             if ((theEvent->modifiers & cmdKey) && (theKey == 0x35)) {
  254.                 // Cmd-Escape is the sequence to temporarily hide floaters
  255.                 //   and a wakeup to handle any pending update events
  256.                 glob.hideFloats = (glob.hideFloats) ? FALSE : TRUE;
  257.                 if (ShowHideFloater(0)) WakeUpProcess(&glob.myPSN);
  258.  
  259.                 fwdThisEvent = filterThisEvent = FALSE;
  260.                 }
  261.             }
  262.             break;
  263.         default:
  264.             break;
  265.         }
  266.     
  267.  
  268.     if (fwdThisEvent) {
  269.         // We have discovered that this event deserves full attention. Forward it to
  270.         //   our home application by Q'ing it up as a pointer in the System Heap
  271.         fwdEvent = (EvQEl *) NewPtrSys(sizeof(*fwdEvent));
  272.         fwdEvent->qLink = 0;
  273.         fwdEvent->qType = evType;
  274.         BlockMove(&theEvent->what, &fwdEvent->evtQWhat, sizeof(EventRecord));
  275.         Enqueue((QElem *) fwdEvent, &glob.forwardedEvents);
  276.         
  277.         // Let our app get the CPU to handle the event quickly
  278.         WakeUpProcess(&glob.myPSN);
  279.         }
  280.         
  281.     // Does intercepting the event mean no one else should have it?
  282.     if (filterThisEvent) theEvent->what = nullEvent;
  283.  
  284.     // Restore the (68K) context before leaving
  285.     SetA5(saveA5);
  286.     }
  287.  
  288.